﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ResourceRecord.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities
{
    #region Usings

    using System;
    using System.Diagnostics.Contracts;
    using System.Globalization;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     Resource Record (rfc1034 3.6.)
    /// </summary>
    public class ResourceRecord
    {
        #region Fields

        /// <summary>
        ///     Private time to live field.
        /// </summary>
        private uint ttl;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceRecord"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public ResourceRecord(IRecordReader recordReader)
        {
            Contract.Requires(recordReader != null);

            this.TimeLived = 0;
            this.Name = recordReader.ReadDomainName();
            this.RRType = (RecordType)recordReader.ReadUInt16();
            this.Class = (ClassificationType)recordReader.ReadUInt16();
            this.TimeToLive = recordReader.ReadUInt32();
            this.Rdlength = recordReader.ReadUInt16();
            this.Record = recordReader.ReadRecord(this.RRType);
            this.Record.ResourceRecord = this;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the name.
        /// </summary>
        /// <remarks>
        ///     The name of the node to which this resource record pertains
        /// </remarks>
        /// <value>The name.</value>
        public string Name { get; private set; }

        /// <summary>
        ///     Gets the DNS type.
        /// </summary>
        /// <value>The type.</value>
        public RecordType RRType { get; private set; }

        /// <summary>
        ///     Gets the record.
        /// </summary>
        /// <remarks>
        ///     One of the Record* classes
        /// </remarks>
        /// <value>The record.</value>
        public Record Record { get; private set; }

        /// <summary>
        ///     Gets or sets the time lived.
        /// </summary>
        /// <value>The time lived.</value>
        public int TimeLived { get; set; }

        /// <summary>
        ///     Gets the time to live.
        /// </summary>
        /// <value>
        ///     The time to live.
        /// </value>
        public uint TimeToLive
        {
            get
            {
                return (uint)Math.Max(0, this.ttl - this.TimeLived);
            }

            private set
            {
                this.ttl = value;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        ///     Gets or sets the class.
        /// </summary>
        /// <remarks>
        ///     Specifies type class of resource record, mostly IN but can be CS, CH or HS
        /// </remarks>
        /// <value>The class.</value>
        private ClassificationType Class { get; set; }

        /// <summary>
        ///     Gets or sets the rdlength.
        /// </summary>
        /// <value>The rdlength.</value>
        private ushort Rdlength { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var rtn = string.Format(
                CultureInfo.InvariantCulture, 
                "{0,-32} {1}\t{2}\t{3}\t{4}", 
                this.Name, 
                this.TimeToLive, 
                this.Class, 
                this.RRType, 
                this.Record);

            return rtn;
        }

        #endregion
    }
}